package com.lql.common.model;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author huangding
 * @description
 * @date 2018/12/4 15:32
 */
@Component
public class BaseComponent {

    @Autowired
    protected HttpServletRequest request;

    @Value("${environment}")
    protected String environment;

    /**
     * 正式环境
     */
    protected final String PROD = "prod";


    /**
     * 判断当前环境
     */
    public boolean isEnvironmentProd() {
        if (environment.equals(PROD)) {
            return true;
        }
        return false;
    }

    /**
     * carNo=" + carNo + ",merCode=" + merCode
     */
    protected Map<String, String> getParameterMap() {
        String parameter = Coder.decode(request.getParameter("parameter"));
        if (StringUtils.isBlank(parameter)) {
            return null;
        }
        Map<String, String> result = new HashMap<>(4);
        String[] split = parameter.split(",");
        for (String str : split) {
            String[] split1 = str.split("=");
            result.put(split1[0], split1[1]);
        }
        return result;
    }

    private ResourceLoader resourceLoader;

    public BaseComponent() {
        resourceLoader = new DefaultResourceLoader();
    }

    /**
     * 检查是否存在空字符串
     */
    public static boolean hasBlank(String... strs) {
        if (strs == null || strs.length == 0) {
            return true;
        } else {
            for (String str : strs) {
                if (StringUtils.isBlank(str)) {
                    return true;
                }
            }
            return false;
        }
    }
}
